﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Text.Json;

namespace SZHelperCore
{
    public class ConsoleHelper:IDisposable
    {
        private string path = "";
        public ConsoleHelper(string _path = "Log")
        {
            //设置工作目录为文件目录
            path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, _path);
            Console.WriteLine("日志路径:"+path);
            //Console.WriteLine(_path);
            //Console.WriteLine("-----------");
            //Console.WriteLine(System.Environment.CurrentDirectory);
            //Console.WriteLine(System.IO.Directory.GetCurrentDirectory());
            //Console.WriteLine(System.AppDomain.CurrentDomain.BaseDirectory);

            if (!Directory.Exists(_path))
            {
                Directory.CreateDirectory(_path);
            }
            path = _path;
        }
        public void WritMsg(ConsoleColor color,string type, params object[] msgs)
        {
            if (msgs == null || msgs.Length == 0) return;
            Console.ForegroundColor = color;

            for (int i = 0; i < msgs.Length; i++)
            {
                var msg = msgs[i];
                string tmp = FormatStr(msg);
                if (i == 0)
                {
                    tmp = AppendMsg(type, tmp);
                }
                else
                {
                    tmp = AppendMsg(tmp);
                }
                Console.WriteLine(tmp);
                WriteLog(tmp);
            }
            Console.ForegroundColor = color;
        }
        public void WriteInfo(params object[] msgs)
        {
            WritMsg(ConsoleColor.Green,"Info",msgs);
        }
        
        public void WriteError(params object[] msgs)
        {
            WritMsg(ConsoleColor.Red, "Error", msgs);
        }
        public void WriteWarning(params object[] msgs)
        {
            WritMsg(ConsoleColor.Yellow, "Warning", msgs);
        }
        private string FormatStr(object obj)
        {
            if (obj == null) return "";
            Type type = obj.GetType();
            if (type.IsPrimitive)
            {
                return  obj.ToString();
            }
            else
            {
                return JsonSerializer.Serialize(obj);
            }
            
        }
        private string AppendMsg(string msg)
        {
            return "\t" + "\t" + "\t" + "\t" + msg;
        }
        private string AppendMsg(string type, string msg)
        {
            return DateTime.Now.ToString() + "\t" + type + "\t" + msg;
        }
        private void WriteLog(string msg)
        {
            try
            {
                string filename = Path.Combine(path , System.DateTime.Now.ToString("d").Replace("/","") + ".txt");
                FileInfo LogWrite = new FileInfo(filename);

                StreamWriter w = LogWrite.AppendText();
                w.WriteLine(msg);
                w.Flush();
                w.Close();
            }
            catch
            {

            }

        }

        public void Dispose()
        {
        }
    }
}
